<template>
  <div class="app-container bg-gray">
    <el-card class="box-1">
      <div slot="header">
        <span>{{ title }}</span>
      </div>
      <el-form ref="form" :model="form" label-width="160px">
        <el-form-item label="课程名称：">
          <el-input v-model="form.name"/>
        </el-form-item>
        <el-form-item label="封面：">
          <ws-upload-single-img v-model="form['coverImgId']" :path="form['coverImgUrl']" width="320" height="180"/>
        </el-form-item>
        <el-form-item label="收费模式：">
          <el-radio-group v-model="form['payMode']" size="small">
            <el-radio label="0">免费</el-radio>
            <el-radio label="1">收费</el-radio>
            <el-radio label="2">会员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="form['payMode']==='1'" label="费用：">
          <el-input v-model="form['payMoney']" style="width: 240px;"/>
          <el-radio-group v-model="form['memberMode']" size="small">
            <el-radio label="0">会员免费</el-radio>
            <el-radio label="1">会员折扣</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="期数：">
          <el-input-number v-model="form.number" />
        </el-form-item>
        <el-form-item label="简介：">
          <el-input v-model="form.brief" type="textarea" rows="4"/>
        </el-form-item>
        <el-form-item label="图文详情：">
          <ws-editor v-model="form['description']"/>
        </el-form-item>
        <div style="text-align:center">
          <el-button type="primary" @click="submitForm('form')">保存</el-button>
          <el-button type="primary" @click="$router.back(-1)">取消</el-button>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        // 页面标题
        title: '',

        form: {
          userId: [],
          payMode: '0',
          memberMode: '0'
        },
        // 允许发送短信剩余秒数
        smsSendSecond: 0,
        errMsg: {
          phone: '',
          code: ''
        },
        // 用户列表
        userList: []
      }
    },
    created() {
      this.title = this.$route.query.title
      if (this.$route.query.id) {
        this.id = this.$route.query.id
        this.getData(this.$route.query.id)
      }
    },
    methods: {
      getData() {
        const url = '../videoAdmin/course/getCourseInfo'
        const data = {id: this.id}
        this.$store.dispatch('GetConnect', { url, data }).then(res => {
          this.form = res.data
        }).catch(e => {
          this.$message.error(e.msg + ',请刷新或联系管理员')
        })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const data = Object.assign({}, this.form);
            const url = this.id ? '../videoAdmin/course/toUpdateCourse' : '../videoAdmin/course/toAddCourse'
            this.$store.dispatch('GetConnect', { url, data }).then(res => {
              this.$message.success(res.msg)
              this.$router.back(-1)
            }).catch((err) => {
              this.$message.error(err.msg)
            })
          } else {
            this.$message.error('提交失败,请检查必填项')
          }
        })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields()
      }
    }
  }
</script>

<style lang="scss" type="text/scss" scoped>
  @import '../../../../styles/index.scss';
  .box-1 {
    max-width: 1000px;
    margin: 0 auto;
    .el-form {
      max-width: 900px;
      .title {
        font-weight: 600;
      }
      .remark {
        font-size: 13px;
        color: $textGray;
      }
      > div > .el-button {
        @extend .bg-green;
        width: 20%;
        min-width: 160px;
      }
    }
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
